<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta property="og:title" content="Node.js使用logger日志" />
<meta property="og:description" content="我们用Node.js写Web API项目的时候，在不用到大型框架，而只是用像Express、Koa等这种微型框架来完成一些小型服务的时候很需要一个日志记录的功能，当然非Web项目也可以使用。
安装 1 yarn add log4js 你也可以使用Npm工具
1 npm i log4js 配置 utils/log.js
这是ES6写法，后续出Typescript (☄⊙ω⊙)☄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 // 引入库 const log4js = require(&#39;log4js&#39;) // 设置配置 log4js.configure({ appenders: { // 选择打印到控制台 ruleConsole: { type: &#39;console&#39; }, // 文件日志 ruleFile: { type: &#39;dateFile&#39;, // 这个目录是相对于根目录的，即与app." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://wuju-i.gitee.io/post/20210617/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-06-17T00:00:00+00:00" />
<meta property="article:modified_time" content="2021-06-17T00:00:00+00:00" />

  <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Node.js使用logger日志"/>
<meta name="twitter:description" content="我们用Node.js写Web API项目的时候，在不用到大型框架，而只是用像Express、Koa等这种微型框架来完成一些小型服务的时候很需要一个日志记录的功能，当然非Web项目也可以使用。
安装 1 yarn add log4js 你也可以使用Npm工具
1 npm i log4js 配置 utils/log.js
这是ES6写法，后续出Typescript (☄⊙ω⊙)☄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 // 引入库 const log4js = require(&#39;log4js&#39;) // 设置配置 log4js.configure({ appenders: { // 选择打印到控制台 ruleConsole: { type: &#39;console&#39; }, // 文件日志 ruleFile: { type: &#39;dateFile&#39;, // 这个目录是相对于根目录的，即与app."/>

  
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#262d33">
  <title>
    
    橘子的博客 - Node.js使用logger日志
    
  </title>
  
  <link rel="shortcut icon" href="g.png" type="image/x-icon" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&display=swap"
    rel="stylesheet">
  
  
  
  <link rel="stylesheet" href="/minima.1714031344.css">

  
  
  <script defer type="text/javascript" src="/minima.1714031344.js"></script>
</head>
<script>
  
  let theme_2b_used = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

  try {
    if (!('theme' in localStorage)) {
      const default_theme = '';
      
      
      if (default_theme === 'dark' || default_theme === 'light') {
        theme_2b_used = default_theme;
      }

      
      localStorage.theme = theme_2b_used;
    }
    
    document.querySelector('html').classList.add(localStorage.theme);
  } catch (e) {
    console.error(e);
  }
</script>


<body class=" sm:mx-5 sm:my-0">
  <header class="flex justify-between items-center mb-6 sm:my-3">
  <div class="flex items-center">
    
    <div id="theme-switcher" class="text-4xl cursor-pointer">🌑</div>
  </div>
  <nav class="flex items-center
    whitespace-nowrap overflow-x-auto overflow-y-hidden">
    
    <a class="ml-5" href="/">首页</a>
    
    <a class="ml-5" href="/categories">分类</a>
    
    <a class="ml-5" href="/log">日记</a>
    
    <a class="ml-5" href="/about">关于</a>
    
  </nav>
</header>
  
  

<h1 class="mt-6 mb-6">Node.js使用logger日志</h1>
<div class="mb-3 text-xs flex justify-between sm:flex-col">
	<div>
	
		Posted at &mdash; 21年 6月17日
	
		
	</div>
	
</div>
<main>
	<p></p>
	<article class="md">
		<p>我们用Node.js写Web API项目的时候，在不用到大型框架，而只是用像Express、Koa等这种微型框架来完成一些小型服务的时候很需要一个日志记录的功能，当然非Web项目也可以使用。</p>
<h2 id="安装">安装</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">yarn add log4js
</span></span></code></pre></td></tr></table>
</div>
</div><p>你也可以使用Npm工具</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">npm i log4js
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="配置">配置</h2>
<p>utils/log.js</p>
<p>这是ES6写法，后续出Typescript (☄⊙ω⊙)☄</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="c1">// 引入库
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">log4js</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;log4js&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 设置配置
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">log4js</span><span class="p">.</span><span class="nx">configure</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">	<span class="nx">appenders</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 选择打印到控制台
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">ruleConsole</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;console&#39;</span>
</span></span><span class="line"><span class="cl">		<span class="p">},</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 文件日志
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">ruleFile</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">type</span><span class="o">:</span> <span class="s1">&#39;dateFile&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 这个目录是相对于根目录的，即与app.js 是同一级的
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="nx">filename</span><span class="o">:</span> <span class="s1">&#39;logs/server&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">			<span class="nx">pattern</span><span class="o">:</span> <span class="s1">&#39;yyyy-MM-dd.log&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 最大大小
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="nx">maxLogSize</span><span class="o">:</span> <span class="mi">10</span> <span class="o">*</span> <span class="mi">1000</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">			<span class="nx">numBackups</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">			<span class="c1">// 是否异步读写
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>			<span class="nx">alwaysIncludePattern</span><span class="o">:</span> <span class="kc">true</span>
</span></span><span class="line"><span class="cl">		<span class="p">}</span>
</span></span><span class="line"><span class="cl">	<span class="p">},</span>
</span></span><span class="line"><span class="cl">	<span class="nx">categories</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">default</span><span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">appenders</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;ruleConsole&#39;</span><span class="p">,</span> <span class="s1">&#39;ruleFile&#39;</span><span class="p">],,</span><span class="nx">level</span><span class="o">:</span> <span class="s1">&#39;info&#39;</span>
</span></span><span class="line"><span class="cl">		<span class="p">}</span>
</span></span><span class="line"><span class="cl">	<span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 注册
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">let</span> <span class="nx">logger</span> <span class="o">=</span> <span class="nx">log4js</span><span class="p">.</span><span class="nx">getLogger</span><span class="p">(</span><span class="s1">&#39;normal&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">export</span> <span class="k">default</span> <span class="nx">logger</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="使用">使用</h2>
<ul>
<li>Node.js</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl">	<span class="kr">const</span> <span class="nx">logger</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;utils/log&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">info</span><span class="p">(</span><span class="s1">&#39;信息&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s1">&#39;错误&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">warn</span><span class="p">(</span><span class="s1">&#39;警告&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">debug</span><span class="p">(</span><span class="s1">&#39;调试&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">fatal</span><span class="p">(</span><span class="s1">&#39;测试&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">	<span class="nx">logger</span><span class="p">.</span><span class="nx">trace</span><span class="p">(</span><span class="s1">&#39;Entering app testing&#39;</span><span class="p">);</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>Express</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl">	<span class="kr">const</span> <span class="nx">log4js</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;log4js&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 配置
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="p">.....</span>
</span></span><span class="line"><span class="cl">	<span class="kr">const</span> <span class="nx">logger</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;utils/log&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 结合Express使用，记录请求日志
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">app</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">log4js</span><span class="p">.</span><span class="nx">connectLogger</span><span class="p">(</span><span class="nx">logger</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="末尾">末尾</h2>
<ul>
<li>log4js日志库地址：<a href="https://github.com/log4js-node/log4js-node">log4js-node</a></li>
<li>log4js日志库文档：<a href="https://log4js-node.github.io/log4js-node/">log4js</a></li>
</ul>

	</article>
</main>





	

	

	



  <footer class="mt-8 flex sm:flex-col-reverse justify-between items-center">
  <p class="mt-0 text-sm">
    © 2022 | 橘子
    
  </p>
  <p class="flex items-center mt-0">
    
  </p>
</footer>
</body>
</html>